package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairWorkdetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbLmtrepairCheckdetailBo;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairCheckdetailVo;
import com.ruoyi.tb.lmt.domain.TbLmtrepairCheckdetail;
import com.ruoyi.tb.lmt.mapper.TbLmtrepairCheckdetailMapper;
import com.ruoyi.tb.lmt.service.ITbLmtrepairCheckdetailService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 质量验收Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@RequiredArgsConstructor
@Service
public class TbLmtrepairCheckdetailServiceImpl implements ITbLmtrepairCheckdetailService {

    private final TbLmtrepairCheckdetailMapper baseMapper;

    @Autowired
    private TbLmtrepairCheckdetailMapper tbLmtrepairCheckdetailMapper;

    /**
     * 查询质量验收
     */
    @Override
    public TbLmtrepairCheckdetailVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询质量验收列表
     */
    @Override
    public TableDataInfo<TbLmtrepairCheckdetailVo> queryPageList(TbLmtrepairCheckdetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbLmtrepairCheckdetail> lqw = buildQueryWrapper(bo);
        Page<TbLmtrepairCheckdetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询质量验收列表
     */
    @Override
    public List<TbLmtrepairCheckdetailVo> queryList(TbLmtrepairCheckdetailBo bo) {
        LambdaQueryWrapper<TbLmtrepairCheckdetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbLmtrepairCheckdetail> buildQueryWrapper(TbLmtrepairCheckdetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbLmtrepairCheckdetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLmtRepairId() != null, TbLmtrepairCheckdetail::getLmtRepairId, bo.getLmtRepairId());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckKind()), TbLmtrepairCheckdetail::getCheckKind, bo.getCheckKind());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUser()), TbLmtrepairCheckdetail::getCheckUser, bo.getCheckUser());
        lqw.eq(bo.getCheckTime() != null, TbLmtrepairCheckdetail::getCheckTime, bo.getCheckTime());
        lqw.like(StringUtils.isNotBlank(bo.getWorkName()), TbLmtrepairCheckdetail::getWorkName, bo.getWorkName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkRequire()), TbLmtrepairCheckdetail::getWorkRequire, bo.getWorkRequire());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkResult()), TbLmtrepairCheckdetail::getWorkResult, bo.getWorkResult());
        lqw.eq(bo.getFinishTime() != null, TbLmtrepairCheckdetail::getFinishTime, bo.getFinishTime());
        lqw.eq(bo.getStartRequireDate() != null, TbLmtrepairCheckdetail::getStartRequireDate, bo.getStartRequireDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbLmtrepairCheckdetail::getWorkGroup, bo.getWorkGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser()), TbLmtrepairCheckdetail::getWorkUser, bo.getWorkUser());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser2()), TbLmtrepairCheckdetail::getWorkUser2, bo.getWorkUser2());
        lqw.eq(bo.getReCheckTime() != null, TbLmtrepairCheckdetail::getReCheckTime, bo.getReCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReCheckUser()), TbLmtrepairCheckdetail::getReCheckUser, bo.getReCheckUser());
        lqw.eq(bo.getLmtRepairWorkId() != null, TbLmtrepairCheckdetail::getLmtRepairWorkId, bo.getLmtRepairWorkId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbLmtrepairCheckdetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkMethod()), TbLmtrepairCheckdetail::getWorkMethod, bo.getWorkMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup2()), TbLmtrepairCheckdetail::getWorkGroup2, bo.getWorkGroup2());
        return lqw;
    }

    /**
     * 新增质量验收
     */
    @Override
    public Boolean insertByBo(TbLmtrepairCheckdetailBo bo) {
        TbLmtrepairCheckdetail add = BeanUtil.toBean(bo, TbLmtrepairCheckdetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改质量验收
     */
    @Override
    public Boolean updateByBo(TbLmtrepairCheckdetailBo bo) {
        TbLmtrepairCheckdetail update = BeanUtil.toBean(bo, TbLmtrepairCheckdetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbLmtrepairCheckdetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除质量验收
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TbLmtrepairCheckdetailBo selectByLmtId(Long lmtId) {
        return tbLmtrepairCheckdetailMapper.selectByLmtId(lmtId);
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectHistoryList(String id, String workKind, String workGroup, String workSelect, String status, Date startTime) {
        return tbLmtrepairCheckdetailMapper.selectWorkHistoryList(id,workKind,workGroup,status,startTime);
    }

    @Override
    public boolean isStayById(Long id) {
        return tbLmtrepairCheckdetailMapper.isStayById(id);
    }

    @Override
    public Long selectIdByLmtWorkId(Long id) {
        return tbLmtrepairCheckdetailMapper.selectIdByLmtWorkId(id);
    }
}
